package HomeWorlk;
/*
1.分析以下需求，并用代码实现：
        (1)定义一个学生类Student，包含属性：姓名(String name)、年龄(int age)
        (2)定义Map集合，用Student对象作为key，用字符串(此表示表示学生的住址)作为value
        (3)利用四种方式遍历Map集合中的内容，格式：key::value
*/

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class test1 {
    public static void main(String[] args) {
        Map<Student, String> mp = new HashMap<>();
        Student t1 = new Student("A小鲁", 22);
        Student t2 = new Student("B中鲁", 22);
        Student t3 = new Student("C大鲁", 22);
        mp.put(t1, "湖南");
        mp.put(t2, "岳阳");
        mp.put(t3, "平江");

        Set<Map.Entry<Student, String>> entries = mp.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            Iterator<Map.Entry<Student, String>> iterator = entries.iterator();
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key + "---" + value);
        }
        System.out.println("---------------------");
        Set<Student> students = mp.keySet();
        for (Student student : students) {
            String s = mp.get(student);
            System.out.println(student + "---" + s);
        }
        System.out.println("---------------------");
        Set<Map.Entry<Student, String>> entries1 = mp.entrySet();
        Iterator<Map.Entry<Student, String>> iterator = entries1.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("---------------------");
        mp.forEach(((student, s) ->{
            System.out.println(student+"----"+s);
        } ));
    }
}

class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
